package pers.xuehongyan.useat.server.manager;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Component;
import pers.xuehongyan.useat.server.enity.Ticket;
import pers.xuehongyan.useat.server.exception.ticket.TicketNotFoundException;
import pers.xuehongyan.useat.server.mapper.TicketMapper;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author xuehongyan
 */
@Component
public class TicketManager {
    @Resource
    TicketMapper ticketMapper;

    public void create(Ticket ticket) {
        ticketMapper.insert(ticket);
    }

    public Ticket getTicketById(Integer id) throws TicketNotFoundException {
        Ticket ticket = ticketMapper.selectById(id);
        if(ticket == null) {
            throw new TicketNotFoundException(id);
        }
        return ticket;
    }

    public List<Ticket> getTicketsByUserId(Integer userId) {
        return ticketMapper.selectList(new QueryWrapper<Ticket>().eq("user_id", userId));
    }

    public void update(Ticket ticket) {
        ticketMapper.updateById(ticket);
    }
}
